package com.woniuxy.config;

import com.woniuxy.common.constant.RedisConstant;
import com.woniuxy.entity.Article;
import com.woniuxy.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * top5预热
 */
@Component
public class InitTop5Runner implements CommandLineRunner {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IArticleService articleService;
    @Override
    public void run(String... args) throws Exception {
        // 查询所有文章
        List<Article> articleList = articleService.list();
        //遍历，加入到Redis的zset集合，分值view_count
        articleList.forEach(article -> {
            redisTemplate.opsForZSet().add(RedisConstant.ARTICLE_LIST,article,article.getViewCount());
        });
    }
}
